package com.sptpc.demo.controller;

import com.sptpc.demo.controller.requests.NewOrderRequest;
import com.sptpc.demo.controller.requests.SearchOrderRequest;
import com.sptpc.demo.model.Drink;
import com.sptpc.demo.model.Order;
import com.sptpc.demo.service.IDrinkService;
import com.sptpc.demo.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    public IOrderService orderService;

    @Autowired
    IDrinkService drinkService;

    //    新增订单
//    request用于接收参数
    @PostMapping("/")
    public Order addNewOrder000(@RequestBody NewOrderRequest request){
        log.info("订单信息：{}",request);
//        要保存的order
        Order order = new Order();
//        和数据库drink里面的id对应起来

        BeanUtils.copyProperties(request,order);
        orderService.addNewOrder000(order,request.getItems());
        return order;
    }

    //    删除订单
    @DeleteMapping("/deleteOrder/{id}")
    public String deleteOrder(@PathVariable Integer id){
        log.info("要删除的订单id: {}",id);
        orderService.deleteOrder(id);
        return "删除订单"+id+"成功";
    }

//    修改订单状态
    @PutMapping("/updateOrder")
    public Order modifyCategory(@RequestBody Order request){
        Order order = orderService.getOrderById(request.getId());
        BeanUtils.copyProperties(request,order);
        orderService.updateOrder(order);
        return order;
    }

    //    新增订单
    @PostMapping("/addOrder1")
    public Order addNewOrder(@RequestBody NewOrderRequest request){
        log.info("订单信息：{}",request);

        Order newOrder = new Order();
        newOrder.setCustomer(request.getCustomer());
        List<Drink> items = drinkService.getDrinkById(request.getItems());
        newOrder.setItems(items);
        newOrder.setState(0);
        newOrder = orderService.addNewOrder(newOrder);
        return newOrder;
    }
    //    新增订单
    @PostMapping("/addOrder")
    public Order addNewOrder1(@RequestBody NewOrderRequest request) {
        Order newOrder = new Order();
        List<Drink> drinks = new ArrayList<>();
//        给money赋初值
        BigDecimal MoneyBigDecimal = BigDecimal.ZERO;
//        通过for循环遍历获取items列对应的drink Id
        for (int i = 0; i < request.items.size(); i++) {
            drinks.add(drinkService.getDrinkById(request.items.get(i)));
        }
//        通过for循环遍历获取items列对应的drink 价格
        for (int i = 0; i < drinks.size(); i++) {
            MoneyBigDecimal = MoneyBigDecimal.add(drinks.get(i).getPrice().getAmount());
        }
//        把要修改的内容=》order对应属性
        BeanUtils.copyProperties(request, newOrder);
//        保存下单的饮品列对象
        newOrder.setItems(drinks);
        newOrder.setCreateTime(LocalDate.now());
        newOrder.setState(0);
//        money是一个特殊对象保存需要通过BigDecimal方法的处理
//        如果我们需要精确计算的结果，则必须使用BigDecimal类来操作。
        newOrder.setPrice(Money.of(CurrencyUnit.of("CNY"),MoneyBigDecimal));
        orderService.addNewOrder(newOrder);
        return newOrder;
    }


    @PostMapping("/search")
    public List<Order> searchOrders(@RequestBody SearchOrderRequest searchOrderRequest) {
        Page<Order> orders = orderService.searchOrders(
                searchOrderRequest,
                PageRequest.of(searchOrderRequest.getPage(), searchOrderRequest.getSize())
        );
        return orders.getContent();
    }



    // get/order/张三
    @GetMapping("/{customer}")
    public List<Order> searchOrdersByCustomer(@PathVariable String customer){
        return orderService.findOrdersByCustomer(customer);
    }

}
